import { request } from '@/utils/request';
import type { LoginRequest, LoginResponseData, RefreshTokenResponse, UserProfile } from '@/types';

export const authApi = {
  // 用户登录
  login: (data: LoginRequest): Promise<LoginResponseData> => {
    return request.post('/auth/login', data);
  },

  // 用户登出
  logout: (): Promise<void> => {
    return request.post('/auth/logout');
  },

  
  // 获取用户详细资料
  getUserProfile: (): Promise<UserProfile> => {
    return request.get('/auth/profile');
  },

  // 更新用户资料
  updateProfile: (data: Partial<UserProfile>): Promise<UserProfile> => {
    return request.put('/auth/profile', data);
  },

  // 上传用户头像
  uploadAvatar: (file: File): Promise<{ avatarUrl: string }> => {
    const formData = new FormData();
    formData.append('avatar', file);
    return request.post('/auth/avatar', formData, {
      headers: {
        'Content-Type': 'multipart/form-data',
      },
    });
  },

  // 获取通知列表
  getNotifications: (params?: { page?: number; size?: number; read?: boolean }): Promise<{
    items: Array<{
      id: string;
      title: string;
      content: string;
      type: 'system' | 'message' | 'alert';
      read: boolean;
      createdAt: string;
    }>;
    total: number;
    unreadCount: number;
  }> => {
    return request.get('/auth/notifications', { params });
  },

  // 标记通知为已读
  markNotificationAsRead: (id: string): Promise<void> => {
    return request.put(`/auth/notifications/${id}/read`);
  },

  // 获取消息列表
  getMessages: (params?: { page?: number; size?: number }): Promise<{
    items: Array<{
      id: string;
      sender: string;
      subject: string;
      content: string;
      read: boolean;
      starred: boolean;
      createdAt: string;
    }>;
    total: number;
    unreadCount: number;
  }> => {
    return request.get('/auth/messages', { params });
  },

  // 刷新token
  refreshToken: (): Promise<RefreshTokenResponse> => {
    return request.post('/auth/refresh-token');
  },

  // 修改密码
  changePassword: (data: { oldPassword: string; newPassword: string }): Promise<void> => {
    return request.post('/auth/change-password', data);
  },
};